<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>sprintf - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/functions/sprintf">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="sprintf.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/functions/sprintf">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/functions/sprintf">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/functions/sprintf">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/functions/sprintf">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/functions/sprintf">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/functions/sprintf">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/functions/sprintf">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/functions/sprintf">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/functions/sprintf">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/functions/sprintf">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/functions/sprintf">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/functions/sprintf">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/functions/sprintf">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/functions/sprintf">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/functions/sprintf">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/functions/sprintf">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/functions/sprintf">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/functions/sprintf">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/functions/sprintf">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/functions/sprintf">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/functions/sprintf">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/functions/sprintf">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/functions/sprintf">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/functions/sprintf">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/functions/sprintf">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/functions/sprintf">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/functions/sprintf">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/functions/sprintf">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/functions/sprintf">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/functions/sprintf">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/functions/sprintf">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/functions/sprintf">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/functions/sprintf">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/functions/sprintf">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/functions/sprintf">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/functions/sprintf">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/functions/sprintf">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/functions/sprintf">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/functions/sprintf">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/functions/sprintf">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/functions/sprintf">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/functions/sprintf">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/functions/sprintf">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/functions/sprintf">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/functions/sprintf">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/functions/sprintf">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/functions/sprintf">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/functions/sprintf">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/functions/sprintf">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/functions/sprintf">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/functions/sprintf">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/functions/sprintf">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/functions/sprintf">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/functions/sprintf">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/functions/sprintf">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/functions/sprintf">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/functions/sprintf">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/functions/sprintf">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/functions/sprintf">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/functions/sprintf">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/functions/sprintf">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/functions/sprintf">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/functions/sprintf">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/functions/sprintf">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/functions/sprintf">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/functions/sprintf">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/functions/sprintf">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/functions/sprintf">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/functions/sprintf">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/functions/sprintf">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/functions/sprintf">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/functions/sprintf">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/functions/sprintf">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/functions/sprintf">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/functions/sprintf">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/functions/sprintf">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/functions/sprintf">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/functions/sprintf">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/functions/sprintf">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/functions/sprintf">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="sprintf.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/functions/sprintf">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/functions/sprintf">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/functions/sprintf">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/functions/sprintf">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/functions/sprintf">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/functions/sprintf">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/functions/sprintf">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/functions/sprintf">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/functions/sprintf">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/functions/sprintf">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/functions/sprintf">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/functions/sprintf">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/functions/sprintf">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/functions/sprintf">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/functions/sprintf">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/functions/sprintf">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/functions/sprintf">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/functions/sprintf">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/functions/sprintf">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/functions/sprintf">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/functions/sprintf">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/functions/sprintf">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/functions/sprintf">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/functions/sprintf">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/functions/sprintf">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/functions/sprintf">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/functions/sprintf">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/functions/sprintf">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/functions/sprintf">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/functions/sprintf">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/functions/sprintf">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/functions/sprintf">5.35.1</a>
          <a class="dropdown-item active" href="sprintf.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="sprintf.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="../perl.html">Perl</a>
          <a class="dropdown-item" href="../perlintro.html">Intro</a>
          <a class="dropdown-item" href="../perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="../perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="../perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="../perlop.html">Operators</a>
          <a class="dropdown-item" href="../functions.html">Functions</a>
          <a class="dropdown-item" href="../variables.html">Variables</a>
          <a class="dropdown-item" href="../modules.html">Modules</a>
          <a class="dropdown-item" href="../perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="../perlcommunity.html">Community</a>
          <a class="dropdown-item" href="../perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="../functions.html">functions</a>
            / <a href="sprintf.html">sprintf</a>
          <div id="more">
            (<a href="sprintf.txt">source</a>,
            <a href="CORE.html#sprintf-FORMAT,-LIST">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
      <dl>

<dt id="sprintf-FORMAT,-LIST"><a class="permalink" href="sprintf.html#sprintf-FORMAT,-LIST">#</a><a id="sprintf"></a><a id="sprintf-FORMAT-LIST"></a>sprintf FORMAT, LIST </dt>
<dd>

<p>Returns a string formatted by the usual <a href="printf.html"><code>printf</code></a> conventions of the C library function <a href="sprintf.html"><code>sprintf</code></a>. See below for more details and see <a href="http://man.he.net/man3/sprintf">sprintf(3)</a> or <a href="http://man.he.net/man3/printf">printf(3)</a> on your system for an explanation of the general principles.</p>

<p>For example:</p>

<pre><code># Format number with up to 8 leading zeroes
my $result = sprintf(&quot;%08d&quot;, $number);

# Round number to 3 digits after decimal point
my $rounded = sprintf(&quot;%.3f&quot;, $number);</code></pre>

<p>Perl does its own <a href="sprintf.html"><code>sprintf</code></a> formatting: it emulates the C function <a href="http://man.he.net/man3/sprintf">sprintf(3)</a>, but doesn&#39;t use it except for floating-point numbers, and even then only standard modifiers are allowed. Non-standard extensions in your local <a href="http://man.he.net/man3/sprintf">sprintf(3)</a> are therefore unavailable from Perl.</p>

<p>Unlike <a href="printf.html"><code>printf</code></a>, <a href="sprintf.html"><code>sprintf</code></a> does not do what you probably mean when you pass it an array as your first argument. The array is given scalar context, and instead of using the 0th element of the array as the format, Perl will use the count of elements in the array as the format, which is almost never useful.</p>

<p>Perl&#39;s <a href="sprintf.html"><code>sprintf</code></a> permits the following universally-known conversions:</p>

<pre><code>%%    a percent sign
%c    a character with the given number
%s    a string
%d    a signed integer, in decimal
%u    an unsigned integer, in decimal
%o    an unsigned integer, in octal
%x    an unsigned integer, in hexadecimal
%e    a floating-point number, in scientific notation
%f    a floating-point number, in fixed decimal notation
%g    a floating-point number, in %e or %f notation</code></pre>

<p>In addition, Perl permits the following widely-supported conversions:</p>

<pre><code>%X    like %x, but using upper-case letters
%E    like %e, but using an upper-case &quot;E&quot;
%G    like %g, but with an upper-case &quot;E&quot; (if applicable)
%b    an unsigned integer, in binary
%B    like %b, but using an upper-case &quot;B&quot; with the # flag
%p    a pointer (outputs the Perl value&#39;s address in hexadecimal)
%n    special: *stores* the number of characters output so far
      into the next argument in the parameter list
%a    hexadecimal floating point
%A    like %a, but using upper-case letters</code></pre>

<p>Finally, for backward (and we do mean &quot;backward&quot;) compatibility, Perl permits these unnecessary but widely-supported conversions:</p>

<pre><code>%i    a synonym for %d
%D    a synonym for %ld
%U    a synonym for %lu
%O    a synonym for %lo
%F    a synonym for %f</code></pre>

<p>Note that the number of exponent digits in the scientific notation produced by <code>%e</code>, <code>%E</code>, <code>%g</code> and <code>%G</code> for numbers with the modulus of the exponent less than 100 is system-dependent: it may be three or less (zero-padded as necessary). In other words, 1.23 times ten to the 99th may be either &quot;1.23e99&quot; or &quot;1.23e099&quot;. Similarly for <code>%a</code> and <code>%A</code>: the exponent or the hexadecimal digits may float: especially the &quot;long doubles&quot; Perl configuration option may cause surprises.</p>

<p>Between the <code>%</code> and the format letter, you may specify several additional attributes controlling the interpretation of the format. In order, these are:</p>

<dl>

<dt id="format-parameter-index"><a class="permalink" href="sprintf.html#format-parameter-index">#</a><a id="format"></a>format parameter index</dt>
<dd>

<p>An explicit format parameter index, such as <code>2$</code>. By default sprintf will format the next unused argument in the list, but this allows you to take the arguments out of order:</p>

<pre><code>printf &#39;%2$d %1$d&#39;, 12, 34;      # prints &quot;34 12&quot;
printf &#39;%3$d %d %1$d&#39;, 1, 2, 3;  # prints &quot;3 1 1&quot;</code></pre>

</dd>
<dt id="flags"><a class="permalink" href="sprintf.html#flags">#</a>flags</dt>
<dd>

<p>one or more of:</p>

<pre><code class="plaintext">space   prefix non-negative number with a space
+       prefix non-negative number with a plus sign
-       left-justify within the field
0       use zeros, not spaces, to right-justify
#       ensure the leading &quot;0&quot; for any octal,
        prefix non-zero hexadecimal with &quot;0x&quot; or &quot;0X&quot;,
        prefix non-zero binary with &quot;0b&quot; or &quot;0B&quot;</code></pre>

<p>For example:</p>

<pre><code>printf &#39;&lt;% d&gt;&#39;,  12;   # prints &quot;&lt; 12&gt;&quot;
printf &#39;&lt;% d&gt;&#39;,   0;   # prints &quot;&lt; 0&gt;&quot;
printf &#39;&lt;% d&gt;&#39;, -12;   # prints &quot;&lt;-12&gt;&quot;
printf &#39;&lt;%+d&gt;&#39;,  12;   # prints &quot;&lt;+12&gt;&quot;
printf &#39;&lt;%+d&gt;&#39;,   0;   # prints &quot;&lt;+0&gt;&quot;
printf &#39;&lt;%+d&gt;&#39;, -12;   # prints &quot;&lt;-12&gt;&quot;
printf &#39;&lt;%6s&gt;&#39;,  12;   # prints &quot;&lt;    12&gt;&quot;
printf &#39;&lt;%-6s&gt;&#39;, 12;   # prints &quot;&lt;12    &gt;&quot;
printf &#39;&lt;%06s&gt;&#39;, 12;   # prints &quot;&lt;000012&gt;&quot;
printf &#39;&lt;%#o&gt;&#39;,  12;   # prints &quot;&lt;014&gt;&quot;
printf &#39;&lt;%#x&gt;&#39;,  12;   # prints &quot;&lt;0xc&gt;&quot;
printf &#39;&lt;%#X&gt;&#39;,  12;   # prints &quot;&lt;0XC&gt;&quot;
printf &#39;&lt;%#b&gt;&#39;,  12;   # prints &quot;&lt;0b1100&gt;&quot;
printf &#39;&lt;%#B&gt;&#39;,  12;   # prints &quot;&lt;0B1100&gt;&quot;</code></pre>

<p>When a space and a plus sign are given as the flags at once, the space is ignored.</p>

<pre><code>printf &#39;&lt;%+ d&gt;&#39;, 12;   # prints &quot;&lt;+12&gt;&quot;
printf &#39;&lt;% +d&gt;&#39;, 12;   # prints &quot;&lt;+12&gt;&quot;</code></pre>

<p>When the # flag and a precision are given in the %o conversion, the precision is incremented if it&#39;s necessary for the leading &quot;0&quot;.</p>

<pre><code>printf &#39;&lt;%#.5o&gt;&#39;, 012;      # prints &quot;&lt;00012&gt;&quot;
printf &#39;&lt;%#.5o&gt;&#39;, 012345;   # prints &quot;&lt;012345&gt;&quot;
printf &#39;&lt;%#.0o&gt;&#39;, 0;        # prints &quot;&lt;0&gt;&quot;</code></pre>

</dd>
<dt id="vector-flag"><a class="permalink" href="sprintf.html#vector-flag">#</a><a id="vector"></a>vector flag</dt>
<dd>

<p>This flag tells Perl to interpret the supplied string as a vector of integers, one for each character in the string. Perl applies the format to each integer in turn, then joins the resulting strings with a separator (a dot <code>.</code> by default). This can be useful for displaying ordinal values of characters in arbitrary strings:</p>

<pre><code>printf &quot;%vd&quot;, &quot;AB\x{100}&quot;;           # prints &quot;65.66.256&quot;
printf &quot;version is v%vd\n&quot;, $^V;     # Perl&#39;s version</code></pre>

<p>Put an asterisk <code>*</code> before the <code>v</code> to override the string to use to separate the numbers:</p>

<pre><code>printf &quot;address is %*vX\n&quot;, &quot;:&quot;, $addr;   # IPv6 address
printf &quot;bits are %0*v8b\n&quot;, &quot; &quot;, $bits;   # random bitstring</code></pre>

<p>You can also explicitly specify the argument number to use for the join string using something like <code>*2$v</code>; for example:</p>

<pre><code>printf &#39;%*4$vX %*4$vX %*4$vX&#39;,       # 3 IPv6 addresses
        @addr[1..3], &quot;:&quot;;</code></pre>

</dd>
<dt id="(minimum)-width"><a class="permalink" href="sprintf.html#(minimum)-width">#</a><a id="minimum-width"></a>(minimum) width</dt>
<dd>

<p>Arguments are usually formatted to be only as wide as required to display the given value. You can override the width by putting a number here, or get the width from the next argument (with <code>*</code>) or from a specified argument (e.g., with <code>*2$</code>):</p>

<pre><code>printf &quot;&lt;%s&gt;&quot;, &quot;a&quot;;       # prints &quot;&lt;a&gt;&quot;
printf &quot;&lt;%6s&gt;&quot;, &quot;a&quot;;      # prints &quot;&lt;     a&gt;&quot;
printf &quot;&lt;%*s&gt;&quot;, 6, &quot;a&quot;;   # prints &quot;&lt;     a&gt;&quot;
printf &#39;&lt;%*2$s&gt;&#39;, &quot;a&quot;, 6; # prints &quot;&lt;     a&gt;&quot;
printf &quot;&lt;%2s&gt;&quot;, &quot;long&quot;;   # prints &quot;&lt;long&gt;&quot; (does not truncate)</code></pre>

<p>If a field width obtained through <code>*</code> is negative, it has the same effect as the <code>-</code> flag: left-justification.</p>

</dd>
<dt id="precision,-or-maximum-width"><a class="permalink" href="sprintf.html#precision,-or-maximum-width">#</a><a id="precision"></a><a id="precision-or-maximum-width"></a>precision, or maximum width </dt>
<dd>

<p>You can specify a precision (for numeric conversions) or a maximum width (for string conversions) by specifying a <code>.</code> followed by a number. For floating-point formats except <code>g</code> and <code>G</code>, this specifies how many places right of the decimal point to show (the default being 6). For example:</p>

<pre><code># these examples are subject to system-specific variation
printf &#39;&lt;%f&gt;&#39;, 1;    # prints &quot;&lt;1.000000&gt;&quot;
printf &#39;&lt;%.1f&gt;&#39;, 1;  # prints &quot;&lt;1.0&gt;&quot;
printf &#39;&lt;%.0f&gt;&#39;, 1;  # prints &quot;&lt;1&gt;&quot;
printf &#39;&lt;%e&gt;&#39;, 10;   # prints &quot;&lt;1.000000e+01&gt;&quot;
printf &#39;&lt;%.1e&gt;&#39;, 10; # prints &quot;&lt;1.0e+01&gt;&quot;</code></pre>

<p>For &quot;g&quot; and &quot;G&quot;, this specifies the maximum number of significant digits to show; for example:</p>

<pre><code># These examples are subject to system-specific variation.
printf &#39;&lt;%g&gt;&#39;, 1;        # prints &quot;&lt;1&gt;&quot;
printf &#39;&lt;%.10g&gt;&#39;, 1;     # prints &quot;&lt;1&gt;&quot;
printf &#39;&lt;%g&gt;&#39;, 100;      # prints &quot;&lt;100&gt;&quot;
printf &#39;&lt;%.1g&gt;&#39;, 100;    # prints &quot;&lt;1e+02&gt;&quot;
printf &#39;&lt;%.2g&gt;&#39;, 100.01; # prints &quot;&lt;1e+02&gt;&quot;
printf &#39;&lt;%.5g&gt;&#39;, 100.01; # prints &quot;&lt;100.01&gt;&quot;
printf &#39;&lt;%.4g&gt;&#39;, 100.01; # prints &quot;&lt;100&gt;&quot;
printf &#39;&lt;%.1g&gt;&#39;, 0.0111; # prints &quot;&lt;0.01&gt;&quot;
printf &#39;&lt;%.2g&gt;&#39;, 0.0111; # prints &quot;&lt;0.011&gt;&quot;
printf &#39;&lt;%.3g&gt;&#39;, 0.0111; # prints &quot;&lt;0.0111&gt;&quot;</code></pre>

<p>For integer conversions, specifying a precision implies that the output of the number itself should be zero-padded to this width, where the 0 flag is ignored:</p>

<pre><code>printf &#39;&lt;%.6d&gt;&#39;, 1;      # prints &quot;&lt;000001&gt;&quot;
printf &#39;&lt;%+.6d&gt;&#39;, 1;     # prints &quot;&lt;+000001&gt;&quot;
printf &#39;&lt;%-10.6d&gt;&#39;, 1;   # prints &quot;&lt;000001    &gt;&quot;
printf &#39;&lt;%10.6d&gt;&#39;, 1;    # prints &quot;&lt;    000001&gt;&quot;
printf &#39;&lt;%010.6d&gt;&#39;, 1;   # prints &quot;&lt;    000001&gt;&quot;
printf &#39;&lt;%+10.6d&gt;&#39;, 1;   # prints &quot;&lt;   +000001&gt;&quot;

printf &#39;&lt;%.6x&gt;&#39;, 1;      # prints &quot;&lt;000001&gt;&quot;
printf &#39;&lt;%#.6x&gt;&#39;, 1;     # prints &quot;&lt;0x000001&gt;&quot;
printf &#39;&lt;%-10.6x&gt;&#39;, 1;   # prints &quot;&lt;000001    &gt;&quot;
printf &#39;&lt;%10.6x&gt;&#39;, 1;    # prints &quot;&lt;    000001&gt;&quot;
printf &#39;&lt;%010.6x&gt;&#39;, 1;   # prints &quot;&lt;    000001&gt;&quot;
printf &#39;&lt;%#10.6x&gt;&#39;, 1;   # prints &quot;&lt;  0x000001&gt;&quot;</code></pre>

<p>For string conversions, specifying a precision truncates the string to fit the specified width:</p>

<pre><code>printf &#39;&lt;%.5s&gt;&#39;, &quot;truncated&quot;;   # prints &quot;&lt;trunc&gt;&quot;
printf &#39;&lt;%10.5s&gt;&#39;, &quot;truncated&quot;; # prints &quot;&lt;     trunc&gt;&quot;</code></pre>

<p>You can also get the precision from the next argument using <code>.*</code>, or from a specified argument (e.g., with <code>.*2$</code>):</p>

<pre><code>printf &#39;&lt;%.6x&gt;&#39;, 1;       # prints &quot;&lt;000001&gt;&quot;
printf &#39;&lt;%.*x&gt;&#39;, 6, 1;    # prints &quot;&lt;000001&gt;&quot;

printf &#39;&lt;%.*2$x&gt;&#39;, 1, 6;  # prints &quot;&lt;000001&gt;&quot;

printf &#39;&lt;%6.*2$x&gt;&#39;, 1, 4; # prints &quot;&lt;  0001&gt;&quot;</code></pre>

<p>If a precision obtained through <code>*</code> is negative, it counts as having no precision at all.</p>

<pre><code>printf &#39;&lt;%.*s&gt;&#39;,  7, &quot;string&quot;;   # prints &quot;&lt;string&gt;&quot;
printf &#39;&lt;%.*s&gt;&#39;,  3, &quot;string&quot;;   # prints &quot;&lt;str&gt;&quot;
printf &#39;&lt;%.*s&gt;&#39;,  0, &quot;string&quot;;   # prints &quot;&lt;&gt;&quot;
printf &#39;&lt;%.*s&gt;&#39;, -1, &quot;string&quot;;   # prints &quot;&lt;string&gt;&quot;

printf &#39;&lt;%.*d&gt;&#39;,  1, 0;   # prints &quot;&lt;0&gt;&quot;
printf &#39;&lt;%.*d&gt;&#39;,  0, 0;   # prints &quot;&lt;&gt;&quot;
printf &#39;&lt;%.*d&gt;&#39;, -1, 0;   # prints &quot;&lt;0&gt;&quot;</code></pre>

</dd>
<dt id="size"><a class="permalink" href="sprintf.html#size">#</a>size</dt>
<dd>

<p>For numeric conversions, you can specify the size to interpret the number as using <code>l</code>, <code>h</code>, <code>V</code>, <code>q</code>, <code>L</code>, or <code>ll</code>. For integer conversions (<code>d u o x X b i D U O</code>), numbers are usually assumed to be whatever the default integer size is on your platform (usually 32 or 64 bits), but you can override this to use instead one of the standard C types, as supported by the compiler used to build Perl:</p>

<pre><code class="plaintext">hh          interpret integer as C type &quot;char&quot; or &quot;unsigned
            char&quot; on Perl 5.14 or later
h           interpret integer as C type &quot;short&quot; or
            &quot;unsigned short&quot;
j           interpret integer as C type &quot;intmax_t&quot; on Perl
            5.14 or later; and prior to Perl 5.30, only with
            a C99 compiler (unportable)
l           interpret integer as C type &quot;long&quot; or
            &quot;unsigned long&quot;
q, L, or ll interpret integer as C type &quot;long long&quot;,
            &quot;unsigned long long&quot;, or &quot;quad&quot; (typically
            64-bit integers)
t           interpret integer as C type &quot;ptrdiff_t&quot; on Perl
            5.14 or later
z           interpret integer as C types &quot;size_t&quot; or
            &quot;ssize_t&quot; on Perl 5.14 or later</code></pre>

<p>Note that, in general, using the <code>l</code> modifier (for example, when writing <code>&quot;%ld&quot;</code> or <code>&quot;%lu&quot;</code> instead of <code>&quot;%d&quot;</code> and <code>&quot;%u&quot;</code>) is unnecessary when used from Perl code. Moreover, it may be harmful, for example on Windows 64-bit where a long is 32-bits.</p>

<p>As of 5.14, none of these raises an exception if they are not supported on your platform. However, if warnings are enabled, a warning of the <a href="../warnings.html"><code>printf</code></a> warning class is issued on an unsupported conversion flag. Should you instead prefer an exception, do this:</p>

<pre><code>use warnings FATAL =&gt; &quot;printf&quot;;</code></pre>

<p>If you would like to know about a version dependency before you start running the program, put something like this at its top:</p>

<pre><code>use 5.014;  # for hh/j/t/z/ printf modifiers</code></pre>

<p>You can find out whether your Perl supports quads via <a href="../Config.html">Config</a>:</p>

<pre><code>use Config;
if ($Config{use64bitint} eq &quot;define&quot;
    || $Config{longsize} &gt;= 8) {
    print &quot;Nice quads!\n&quot;;
}</code></pre>

<p>For floating-point conversions (<code>e f g E F G</code>), numbers are usually assumed to be the default floating-point size on your platform (double or long double), but you can force &quot;long double&quot; with <code>q</code>, <code>L</code>, or <code>ll</code> if your platform supports them. You can find out whether your Perl supports long doubles via <a href="../Config.html">Config</a>:</p>

<pre><code>use Config;
print &quot;long doubles\n&quot; if $Config{d_longdbl} eq &quot;define&quot;;</code></pre>

<p>You can find out whether Perl considers &quot;long double&quot; to be the default floating-point size to use on your platform via <a href="../Config.html">Config</a>:</p>

<pre><code>use Config;
if ($Config{uselongdouble} eq &quot;define&quot;) {
    print &quot;long doubles by default\n&quot;;
}</code></pre>

<p>It can also be that long doubles and doubles are the same thing:</p>

<pre><code>use Config;
($Config{doublesize} == $Config{longdblsize}) &amp;&amp;
        print &quot;doubles are long doubles\n&quot;;</code></pre>

<p>The size specifier <code>V</code> has no effect for Perl code, but is supported for compatibility with XS code. It means &quot;use the standard size for a Perl integer or floating-point number&quot;, which is the default.</p>

</dd>
<dt id="order-of-arguments"><a class="permalink" href="sprintf.html#order-of-arguments">#</a><a id="order"></a>order of arguments</dt>
<dd>

<p>Normally, <a href="sprintf.html"><code>sprintf</code></a> takes the next unused argument as the value to format for each format specification. If the format specification uses <code>*</code> to require additional arguments, these are consumed from the argument list in the order they appear in the format specification <i>before</i> the value to format. Where an argument is specified by an explicit index, this does not affect the normal order for the arguments, even when the explicitly specified index would have been the next argument.</p>

<p>So:</p>

<pre><code>printf &quot;&lt;%*.*s&gt;&quot;, $a, $b, $c;</code></pre>

<p>uses <code>$a</code> for the width, <code>$b</code> for the precision, and <code>$c</code> as the value to format; while:</p>

<pre><code>printf &#39;&lt;%*1$.*s&gt;&#39;, $a, $b;</code></pre>

<p>would use <code>$a</code> for the width and precision, and <code>$b</code> as the value to format.</p>

<p>Here are some more examples; be aware that when using an explicit index, the <code>$</code> may need escaping:</p>

<pre><code>printf &quot;%2\$d %d\n&quot;,      12, 34;     # will print &quot;34 12\n&quot;
printf &quot;%2\$d %d %d\n&quot;,   12, 34;     # will print &quot;34 12 34\n&quot;
printf &quot;%3\$d %d %d\n&quot;,   12, 34, 56; # will print &quot;56 12 34\n&quot;
printf &quot;%2\$*3\$d %d\n&quot;,  12, 34,  3; # will print &quot; 34 12\n&quot;
printf &quot;%*1\$.*f\n&quot;,       4,  5, 10; # will print &quot;5.0000\n&quot;</code></pre>

</dd>
</dl>

<p>If <a href="../locale.html"><code>use locale</code></a> (including <code>use locale &#39;:not_characters&#39;</code>) is in effect and <a href="../POSIX.html#setlocale"><code>POSIX::setlocale</code></a> has been called, the character used for the decimal separator in formatted floating-point numbers is affected by the <code>LC_NUMERIC</code> locale. See <a href="../perllocale.html">perllocale</a> and <a href="../POSIX.html">POSIX</a>.</p>

</dd>
</dl>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
